<template>
  <el-card class="mt-5">
    <template #header>
      <el-button icon="Plus" type="primary" @click="onAdd()">添加用户</el-button>
      <el-button icon="Edit" type="warning" :disabled="selectedList.length != 1" @click="onEdit()">编辑用户</el-button>
      <el-button icon="Delete" type="danger" :disabled="selectedList.length <= 0"
        @click="onBatchDelete()">删除用户</el-button>
    </template>
    <el-table :data="list" @selection-change="tableSelectChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column prop="username" label="账号" align="center" />
      <el-table-column prop="email" label="邮箱" align="center" />
      <el-table-column prop="avatar" label="头像" align="center" />
      <el-table-column prop="status" label="状态" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.status ? 'primary' : 'danger'">{{ scope.row.status ? '启用' : '禁用' }}</el-tag>
          <span></span>
        </template>
      </el-table-column>
      <el-table-column prop="created_time" label="创建时间" align="center" />
      <el-table-column fixed="right" label="操作" min-width="90" align="center">
        <template #default="scope">
          <el-button link type="warning" icon="Edit" @click="onEdit(scope.row.id)">编辑</el-button>
          <el-button link type="danger" icon="Delete" @click="onRemove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="flex flex-justify-end">
        <el-pagination background layout="total, sizes, prev, pager, next, jumper" v-model:current-page="page_index"
          v-model:page-size="page_size" :page-sizes="[10, 20, 30, 40]" :total="total" @size-change="pageSizeChange"
          @current-change="pageIndexChange" />
      </div>
    </template>
  </el-card>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import useSystemStore from '@/store/system';
import { storeToRefs } from "pinia"
import type { UserModel } from '@/types';

const emit = defineEmits(["on-add", "on-edit"])
const systemStore = useSystemStore()

var { userList, userTotal } = storeToRefs(systemStore)
// 表格数据
var list = ref(userList)
// 表格已选择的数据
var selectedList = ref<UserModel[]>([])
// 总条数
var total = ref(userTotal)
// 分页数据
var page_size = ref(10)
var page_index = ref(1)

// 添加
function onAdd() {
  emit("on-add")
}
// 编辑
async function onEdit(id?: number) {
  const result = await systemStore.getUserAction(id)
  emit("on-edit", result)
}
// 删除
function onRemove(id: number) {
  console.log("删除：" + id)
}
// 批量删除
function onBatchDelete() {
  console.log("批量删除")
}
// 页码改变
function pageIndexChange() {
  getTableData()
}
// 每页显示条数改变
function pageSizeChange(size: number) {
  getTableData()
}
// 表格数据选择改变
function tableSelectChange(rows: any[]) {
  console.log("表格数据选择改变：")
  selectedList.value = rows
}

// 请求table数据
function getTableData(queryParams?: any) {
  var pageParams = { page_size: page_size.value, page_index: page_index.value }
  var requestParams = { ...pageParams, ...queryParams }
  systemStore.getUsersAction(requestParams)
}

getTableData();

defineExpose({ getTableData })
</script>

<style lang="scss" scoped></style>
